//
//  ActivityViewController.m
//  xpkc
//
//  Created by TopSage iOS on 15/2/2.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "ActivityViewController.h"
#import "preferentialModel.h"
#import "MBProgressHUD.h"
#import "MyNotificationCell.h"
@interface ActivityViewController (){
    NSMutableArray *activitiesArray;
    UITableView *actTab;
}

@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"最新优惠活动";
    self.navigationController.navigationBar.hidden = NO;
    activitiesArray = [NSMutableArray array];
    [self creatQAView];
    [self requestActivity];
}
- (void)viewDidAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)creatQAView{
    
    actTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 50) style:UITableViewStylePlain];
    actTab.delegate = self;
    actTab.dataSource = self;
    actTab.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:actTab];
    
   
}
#pragma mark - tab delegste
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return activitiesArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[MyNotificationCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    preferentialModel * model =activitiesArray[indexPath.row];
    cell.title.text= model.act_title;
    cell.content.text = model.act_desc;
    cell.time.text=[NSString stringWithFormat:@"%@-%@",model.stime,model.etime];
    cell.time.textAlignment=NSTextAlignmentRight;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    preferentialModel * model =activitiesArray[indexPath.row];
    NSString *urlString=model.act_url;
    ActivityContentViewController *ACVC = [[ActivityContentViewController alloc]init];
    ACVC.url = urlString;
    ACVC.title = @"活动详情";
    [self.navigationController pushViewController:ACVC animated:YES];
}

#pragma mark - func

- (void)requestActivity{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中";
    NSString *module = @"user";
    NSString *interface = @"newacts";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    NSDictionary *parameter = @{@"time":timeString};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        
        if ([dic objectForKey:@"succ"])
        {  NSArray * arr =[dic objectForKey:@"ActsList"];
           for (NSDictionary * dic in arr) {
            preferentialModel * model =[[preferentialModel alloc] init];
            model.act_url=[dic objectForKey:@"act_url"];
            model.act_title=[dic objectForKey:@"act_title"];
            model.act_desc=[dic objectForKey:@"act_desc"];
               model.stime=[dic objectForKey:@"stime"];
               model.etime=[dic objectForKey:@"etime"];
            [activitiesArray addObject:model];
             }
        }else{
            
        }

        [actTab reloadData];
        [hud hide:YES];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [hud hide:YES];

        logdebug(@"%@",error.localizedDescription);
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
